package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.domain.Questionnaire;
import com.example.domain.User;
import com.example.mapper.QuestionnaireMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author DPH
* @description 针对表【tbl_user】的数据库操作Service实现
* @createDate 2022-11-23 16:21:29
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private QuestionnaireMapper questionnaireMapper;

    @Override
    public boolean register(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean login(String username, String password) {
        User user = userMapper.selectById(username);
        return user != null && user.getPassword().equals(password);
    }

    @Override
    public User findByUsername(String username) {
        return userMapper.selectById(username);
    }

    @Override
    public List<Questionnaire> getQuestionnaire(String username) {
        QueryWrapper<Questionnaire> wrapper = new QueryWrapper<>();
        wrapper.ge("creator", username).orderByAsc("startTime");
        return questionnaireMapper.selectList(wrapper);
    }

    @Override
    public User modify(User user) {
        return userMapper.updateById(user) > 0 ? user : null;
    }

    @Override
    public IPage getUsers(int currentPage, int pageSize) {
        IPage page = new Page(currentPage,pageSize);
        userMapper.selectPage(page,null);
        return page;
    }

    @Override
    public int getUserCount() {
        return Math.toIntExact(userMapper.selectCount(null));
    }


}



